package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmbusinesstrip extends X_hrm_businesstrip {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1453631435270720737L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmbusinesstrip.class);

	public Mhrmbusinesstrip(Properties ctx) {
		super(ctx);
	}
	
	public Mhrmbusinesstrip(Properties ctx, int hrm_businesstrip_ID,
			String trxName) {
		super(ctx, hrm_businesstrip_ID, trxName);
	}

	public Mhrmbusinesstrip(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}
	
	public static List<Mhrmbusinesstrip> getBusinessTripLines(BigDecimal HRM_BusinessTrip_ID){
		List<Mhrmbusinesstrip> list = new ArrayList<Mhrmbusinesstrip>();
		
//		Integer HRM_BusinessTrip_ID = (Integer)value;
//		if (isCalloutActive () // assuming it is resetting value
//			if (HRM_BusinessTrip_ID == null || HRM_BusinessTrip_ID.intValue () == 0)
//			return null;
//		
//			
////			ArrayList<MFI_C_PaymentLine> lines = new ArrayList<MFI_C_PaymentLine>();
//			
//			//get HRM_BusinessTripLine's-------------------------{
//			String sql = "select hrm_businesstrip_line_id, c_bpartner_id, hrm_total " +
//					 "	from adempiere.hrm_businesstrip_line " +
//					 "	where hrm_businesstrip_id = ? " +
//					 "	order by hrm_businesstrip_line_id";
//			PreparedStatement pstmt = null;
//			ResultSet rs = null;
//			try
//			{
//				pstmt = DB.prepareStatement (sql, null);
//				pstmt.setBigDecimal(1, HRM_BusinessTrip_ID);
//				rs = pstmt.executeQuery ();
//				while (rs.next ())
//				{
//					
//					list.setC_BPartner_ID(rs.getInt(2));
//					line.setAmount(rs.getBigDecimal(3));
//					line.save();
//				}
//			}
//			catch (SQLException e)
//			{
//				log.log (Level.SEVERE, sql, e);
//				return e.getLocalizedMessage ();
//			}
//			catch(Exception e){
//				log.log (Level.SEVERE, sql, e);
//				return e.getLocalizedMessage ();
//			}
//			finally
//			{
//				DB.close (rs, pstmt);
//				rs = null;
//				pstmt = null;
//			}

		
		return list;
	}

	public static Mhrmbusinesstrip getTripRec(Properties ctx, int hrm_order_id){
		
		if (hrm_order_id == 0)
			return null;
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmbusinesstrip result = new Mhrmbusinesstrip(Env.getCtx(), 0, null);
			
		// 
		String sql_ = "SELECT * FROM hrm_businesstrip \n " +
						" WHERE ISACTIVE='Y' \n "  +
						   " AND AD_CLIENT_ID="  + Env.getAD_Client_ID(ctx) + " \n " + 
						   " AND hrm_order_id=" + hrm_order_id  ;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new Mhrmbusinesstrip(Env.getCtx(), rs, null);
				

			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	}
	
	/**	Businesstrip Lines	*/
	private Mhrmbusinesstripline[] m_lines;
	
	/**
	 * 	Get HR Businesstrip Lines
	 * 	@param whereClause starting with AND
	 * 	@return lines
	 */
	private Mhrmbusinesstripline[] getLinesTrip (String whereClause)
	{
		String whereClauseFinal = "hrm_businesstrip_ID=? ";
		if (whereClause != null)
			whereClauseFinal += whereClause;
		List<Mhrmbusinesstripline> list = new Query(getCtx(), I_hrm_businesstrip_line.Table_Name, whereClauseFinal, get_TrxName())
										.setParameters(gethrm_businesstrip_ID())
										.list();
		return list.toArray(new Mhrmbusinesstripline[list.size()]);
	}	//	getLines

	/**
	 * 	Get Businesstrip Lines
	 * 	@param requery
	 * 	@return lines
	 */
	public Mhrmbusinesstripline[] getLinesTrip (boolean requery)
	{
		if (m_lines == null || m_lines.length == 0 || requery)
			m_lines = getLinesTrip(null);
		set_TrxName(m_lines, get_TrxName());
		return m_lines;
	}	//	getLines

	/**
	 * 	Get Businesstrip line
	 * 	@return lines
	 */
	public Mhrmbusinesstripline[] getLinesTrip()
	{
		return getLinesTrip(false);
	}	//	getLines

	
	public static BigDecimal[] getCompensation(int bpartnerID, Timestamp dt){
		
		ArrayList<BigDecimal> result = new ArrayList<BigDecimal>();
		BigDecimal value = new BigDecimal(0);
		
		String sql = "select hrm_total from hrm_invoicetrip \n" +
					 "where c_bpartner_id="+bpartnerID+" \n" +
					 "and to_char(datefrom, 'YYYY-MM') = to_char("+DB.TO_DATE(dt)+",'YYYY-MM')";
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			while(rs.next()){
				value = rs.getBigDecimal(1);
				result.add(value);
			}
		}
		catch (SQLException e)
		{
			CLogger.get().log(Level.INFO, "SQL=" + sql, e);
		}
		finally {
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		return result.toArray(new BigDecimal[result.size()]);
	}
	
}
